using IQIGame.Onigao.Framework;
using IQIGame.Onigao.Game;

namespace IQIGame.Onigao.GamePlay
{
    public class UIN_OneMoney : UIBaseNode
    {

        #region Template Generate,don't modify
        protected partial class UIB_UIN_OneMoney
        {
            #region ObjectBinding Generate 
            public IQIGame.Onigao.Framework.ExButton buttonIcon { protected set; get; }
            public IQIGame.Onigao.Framework.ExButton buttonAdd { protected set; get; }
            public IQIGame.Onigao.Framework.ExImage bG { protected set; get; }
            public IQIGame.Onigao.Framework.ExImage icon { protected set; get; }
            public IQIGame.Onigao.Framework.ExText textNum { protected set; get; }
            public virtual void InitBinding(ObjectBinding __binding)
            {
                __binding.TryGetVariableValue<IQIGame.Onigao.Framework.ExButton>("buttonIcon", out var __tbv0);
                this.buttonIcon = __tbv0;
                __binding.TryGetVariableValue<IQIGame.Onigao.Framework.ExButton>("buttonAdd", out var __tbv1);
                this.buttonAdd = __tbv1;
                __binding.TryGetVariableValue<IQIGame.Onigao.Framework.ExImage>("bG", out var __tbv2);
                this.bG = __tbv2;
                __binding.TryGetVariableValue<IQIGame.Onigao.Framework.ExImage>("icon", out var __tbv3);
                this.icon = __tbv3;
                __binding.TryGetVariableValue<IQIGame.Onigao.Framework.ExText>("textNum", out var __tbv4);
                this.textNum = __tbv4;
            }

            #endregion ObjectBinding Generate 
        }
        #endregion Template Generate,don't modify

        #region fields & properties
        public int cid { get; private set; }
        protected UIB_UIN_OneMoney ui { get; set; }
        private UIMsgDispatcher currMsgDispatcher => this._msgDispatcher as UIMsgDispatcher;
        #endregion

        protected override void BeforeInit()
        {
            ui = new UIB_UIN_OneMoney();
            ui.InitBinding(this.csObjBind);
        }

        protected override void OnInit()
        {
            this.ui.buttonAdd.onClick.AddListener(this.OnClickAdd);
            this.ui.buttonIcon.onClick.AddListener(this.OnClickIcon);
        }

        protected override void OnShow()
        {

        }

        protected override void OnHide()
        {

        }

        protected override void OnDispose()
        {

        }

        protected override void AddMsgListeners()
        {
            currMsgDispatcher.AddListener<int>(MsgEventType.OneItemChanged, this.OnMsgOneItemChanged);
        }

        void OnMsgOneItemChanged(int cid)
        {
            if (cid == this.cid)
            {
                this.ViewNum();
            }
        }

        public void Show(int cid)
        {
            this.cid = -1;
            if (cid == 0)
                return;

            var cfg = TableCenter.item.Get(cid);
            if (cfg == null)
                return;
            this.cid = cid;
            this.Show();

            string assetIcon = string.IsNullOrEmpty(cfg.MoneyIcon) ? cfg.Icon_fullPath : cfg.MoneyIcon_fullPath;
            this.SetImageSprite(this.ui.icon, assetIcon);
            this.ViewNum();
        }

        void ViewNum()
        {
            int num = ItemModule.Instance.GetNum(this.cid);
            this.ui.textNum.text = num.ToString();
        }

        void OnClickAdd()
        {
            var cfg = TableCenter.item.Get(this.cid);
            // TODO 单击 Money Add
        }

        void OnClickIcon()
        {
            var cfg = TableCenter.item.Get(this.cid); ;
            // TODO 单击 Money Icon
        }
    }
}
